package com.atom.common.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.atom.common.entity.HistoricTaskDO;
import com.atom.common.entity.bo.TaskLogBO;
import com.atom.common.exception.AvensIllegalArgumentException;

@Repository
public class HistoricTaskDao extends AvensCommonDao<HistoricTaskDO> {  
  
  /**
   * Description: 根据用户名查询已办
   * @param userId
   * @return
   */
  public List<HistoricTaskDO> findByUserId(String userId) {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("assignee", userId);
    return super.findByProperties(properties);
  }
  
  @SuppressWarnings("unchecked")
  public List<TaskLogBO> findTaskLogList(String mainProcessInstanceId) {
    StringBuffer query = new StringBuffer();
    query.append("SELECT id as id,");
    query.append("finishTime as finishTime,");
    query.append("processInstanceName as processDefinitionName,");
    query.append("taskName as taskName,");
    query.append("taskDefinitionKey as taskDefinitionKey,");
    query.append("assignee as assignee from ");
    query.append(HistoricTaskDO.class.getName());
    query.append(" WHERE MAIN_PROC_INS_ID = '");
    query.append(mainProcessInstanceId);
    query.append("'"); 
    return super.executeQueryGetBeanResultByPage(query.toString(), TaskLogBO.class, null, null);
  }

  public List<HistoricTaskDO> findByMainProcessInstanceIdByDefinitionKey(
      String mainProcessInstanceId, String taskDefinitionKey) {
    if (mainProcessInstanceId == null || taskDefinitionKey == null) {
      throw new AvensIllegalArgumentException("mainProcessInstanceId or taskDefinitionKey is null");
    }
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("mainProcessInstanceId", mainProcessInstanceId);
    properties.put("taskDefinitionKey", taskDefinitionKey);
    return super.findByProperties(properties);
  }
  


}
